<?php

namespace app\admin\model;

use think\Model;

/**
 * 知识分类
 * Class ArticleCate
 * @package app\admin\model
 */
class ArticleCate extends Model
{
    /**
     * 知识分类列表
     * @param $where
     * @param $page
     * @param $limit
     * @return array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getArticleCateList($where, $page, $limit)
    {
        $list = self::where($where)
            ->order(['sort' => 'desc', 'create_time' => 'desc'])
            ->where('status', '<>', -1)
            ->page($page, $limit)
            ->select();

        return $list ? $list->toArray() : [];
    }

    /**
     * 知识分类统计
     * @param $where
     * @return int
     */
    public static function countArticleCateList($where)
    {
        $count = self::where($where)
            ->where('status', '<>', -1)
            ->count();
        return $count;
    }


    /**
     * 获取指定知识分类
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findArticleCate($where)
    {
        $info = self::where($where)->find();
        return $info ? $info->toArray() : [];
    }

    /**
     * 知识分类更新
     * @param $data
     * @param $where
     * @return ArticleCate
     */
    public static function updateArticleCate($data, $where)
    {
        return self::update($data, $where);
    }

    /**
     * 知识分类添加
     * @param $data
     * @return ArticleCate|Model
     */
    public static function createArticleCate($data)
    {
        return self::create($data);
    }

    /**
     * 所有知识分类
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAllArticleCate()
    {
        $data = self::where('status', '<>', -1)->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 分类名
     * @param $where
     * @return mixed
     */
    public static function findArticleCateName($where)
    {
        return self::where($where)->value('name');
    }

}